{ "cells": [ { "cell_type": "markdown", "id": "31e14143", "metadata": {}, "source": [ "## Sort a list" ] }, { "cell_type": "markdown", "id": "a66ff918", "metadata": {}, "source": [ "### You can use .sort() or sorted() methods" ] }, { "cell_type": "markdown", "id": "ee8a5afc", "metadata": {}, "source": [ "| list.Sort() | sorted(list) |\n", "|---|---|\n", "| <b>Difference:</b> sorts the list in-place and <b>change the original list</b> & returns None | takes a list,(or any iterable) ,sort it & <b>returns a new list,sorted </b> |\n", "| Works only on lists | Works with any iterable(str,list,dict,tupple) |\n", "| <b>Syntax:</b> list.sort([key=...], [reverse=True/False)] | new_list=sorted(list, [key=...], [reverse=True/False)] |\n", "| <b>Explanation:</b> Since in place so no need to store anywhere and use . | Since creates a new list so we need to store it somewhere and org one is passed as args |" ] }, { "cell_type": "markdown", "id": "8d0e9a26", "metadata": {}, "source": [ "### Example of using sort()" ] }, { "cell_type": "code", "execution_count": 28, "id": "c7207e98", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n", "[1, 2, 3]\n" ] } ], "source": [ "a = [3, 2, 1]\n", "print (a.sort()) # in place\n", "print (a) # it's modified" ] }, { "cell_type": "markdown", "id": "8a121ecb", "metadata": {}, "source": [ "### Example of using sorted()" ] }, { "cell_type": "code", "execution_count": 29, "id": "14d5206b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "[3, 2, 1]\n" ] } ], "source": [ "a = [3, 2, 1]\n", "print (sorted(a)) # new list,you will need to store it somewhere\n", "print (a) # is not modified" ] }, { "cell_type": "markdown", "id": "bd6daacf", "metadata": {}, "source": [ "### Keys" ] }, { "cell_type": "code", "execution_count": null, "id": "7ff19797", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "857fbc7d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "89a9487b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b0f63745", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "eb8fbb75", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 4, "id": "4fd8fcaa", "metadata": {}, "outputs": [], "source": [ "names=['Sahil','Sonia','Abhi','Sourav']\n", "age=[10,20,30]" ] }, { "cell_type": "code", "execution_count": 5, "id": "44710703", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Abhi', 'Sahil', 'Sonia', 'Sourav']\n" ] } ], "source": [ "names.sort()\n", "print(names)" ] }, { "cell_type": "markdown", "id": "a1052168", "metadata": {}, "source": [ "### What if repeated letters?" ] }, { "cell_type": "code", "execution_count": 17, "id": "bfffbfa5", "metadata": {}, "outputs": [], "source": [ "l=['A','B','C','D','A','AA']\n", "l.sort()" ] }, { "cell_type": "code", "execution_count": 18, "id": "1b439bb1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['A', 'A', 'AA', 'B', 'C', 'D']" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": null, "id": "744383bb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7c3f4374", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "93a5f3bc", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }